package com.woniu.controller;


import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.woniu.model.AllRoomnum;
import com.woniu.model.Result;
import com.woniu.utils.RedisKeyUtil;
import com.woniu.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jas
 * @since 2024年06月05日
 */
@RestController
@RequestMapping("/allRoomnum")
@Api(tags = "所有房间号控制层")
public class AllRoomnumController {
    @Resource
    private RedisUtil redisUtil;
    @Resource
    private ObjectMapper objectMapper;
    @GetMapping("/loadAllRoomNum")
    @ApiOperation("查看所有房间号")
    public Result<List<AllRoomnum>> loadAllRoomNum() throws JsonProcessingException {
        List<AllRoomnum> allRoomnums = new ArrayList<>();
        if (redisUtil.exist(RedisKeyUtil.allUserIdAndRoomNum())) {
            List<String> collect = redisUtil.zrevrange(RedisKeyUtil.allUserIdAndRoomNum(), 0, -1).stream().collect(Collectors.toList());
            for (String s : collect) {
                //转换为allRoomnum对象
                AllRoomnum allRoomnum = objectMapper.readValue(s, AllRoomnum.class);
                allRoomnums.add(allRoomnum);
            }
            return Result.newListInstance(AllRoomnum.class).setData(allRoomnums);
        }
        return Result.newListInstance(AllRoomnum.class).setData(allRoomnums);
    }
}

